package com.nbgroup.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nbgroup.context.BaseContext;
import com.nbgroup.exception.DeletionNotAllowedException;
import com.nbgroup.mapper.RoleMapper;
import com.nbgroup.mapper.SystemMapper;
import com.nbgroup.mapper.TransferMapper;
import com.nbgroup.pojo.Dept;
import com.nbgroup.pojo.DeptMent;
import com.nbgroup.pojo.Role;
import com.nbgroup.pojo.User;
import com.nbgroup.pojo.common.PageResult;
import com.nbgroup.pojo.common.UserExportExcel;
import com.nbgroup.pojo.dto.UserDTO;
import com.nbgroup.pojo.vo.DepartmentVo;
import com.nbgroup.pojo.vo.RoleVo;
import com.nbgroup.pojo.vo.UserVo;
import com.nbgroup.service.SystemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@Slf4j
public class SystemServiceImpl implements SystemService {

    @Autowired
    private SystemMapper systemMapper;
    @Autowired
    private TransferMapper transferMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserExportExcel userExportExcel;
    /**
     * 查询用户
     */
    @Override
    public PageResult queryUser(UserDTO dto) {
        log.info("分页查询用户,{}",dto);
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<User> list=systemMapper.queryUser(dto);
        List<Integer> list1 = list.stream().map(User::getDeptId).collect(Collectors.toList());
        /*ArrayList<DepartmentVo> depts = transferMapper.selectDepartment(list1);
        for (User user : list) {
            List<DepartmentVo> collect = depts.stream().filter(l -> l.getId().equals(user.getDeptId())).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(collect)){
                continue;
            }
            DepartmentVo departmentVo = collect.get(0);
            Dept dept = new Dept();
            dept.setDeptId(departmentVo.getId().intValue());
            BeanUtils.copyProperties(departmentVo,dept);
            dept.setDeptName(dept.getDeptName());
            user.setDept(dept);
        }
        list1.get(0);*/
        Page<User> p= (Page<User>) list;
        return new PageResult(200,"查询成功",p.getTotal(),p.getResult(),null);
    }

    /**
     * 新增用户
     */
    @Override
    public void addUser(User user) {
        user.setCreateTime(LocalDateTime.now());
        user.setCreateBy(BaseContext.getCurrentId().toString());
        String roles = StringUtils.collectionToCommaDelimitedString(user.getRoleIds());
        user.setRoleIs(roles);
        if(user.getPassword()==null){
            user.setPassword("123456");
        }
        systemMapper.addUser(user);
    }

    /**
     *查询角色
     */
    @Override
    public RoleVo queryAllRole() {
        RoleVo roleVo = new RoleVo();
        List<Role> list=systemMapper.queryAllRole();
        roleVo.setRoles(list);
        roleVo.setMsg("查询成功");
        roleVo.setCode(200);
        roleVo.setPosts("admin");
        return roleVo;
    }

    /**
     * 根据id查询用户
     */
    @Override
    public UserVo genInfoUser(Integer id) {
        UserVo userVo = new UserVo();
        User user=systemMapper.genInfoUser(id);
        userVo.setData(user);
        userVo.setCode(200);
        userVo.setPosts(null);
        user.setPostId(null);
        userVo.setMsg("查询成功");
        List<Integer> list = new ArrayList<>();
        String[] split = user.getRoleIs().split(",");
        for (String s : split) {
            list.add(Integer.parseInt(s));
        }
        userVo.setRoleIds(list);
        List<Role> roles = roleMapper.queryAllRole();
        userVo.setRoles(roles);
        return userVo;
    }

    /**
     * 修改用户
     */
    @Override
    public void updateUser(User user) {
        systemMapper.updateUser(user);
    }

    /**
     * 删除用户
     */
    @Override
    public void deleteUser(Integer id) {
        User user=systemMapper.genInfoUser(id);


        systemMapper.deleteUser(id);
    }

    /**
     * 重置密码
     */
    @Override
    public void resetPwd(User user) {
        systemMapper.resetPwd(user);
    }

    /**
     * 用户数据导出
     * @param userDTO
     */
    @Override
    public void userExport(UserDTO userDTO) {
        Integer pageNum = userDTO.getPageNum();
        Integer pageSize = userDTO.getPageSize();
        int num = (pageNum - 1) * pageSize;
        int num1 = pageNum * pageSize;
        List<User> users = systemMapper.userExport(num,num1);
        userExportExcel.exportRole(users);
    }
}
